package ${businessPackagePath}.service;


import ${businessPackagePath}.service.${changeTableName}Service;
import ${businessPackagePath}.mapper.${changeTableName}Mapper;
import ${businessPackagePath}.convert.${changeTableName}BeanConvert;
import ${entityPackagePath}.${changeTableName};
import ${entityPackagePath}.${changeTableName}Vo;
import ${entityPackagePath}.${changeTableName}InsertDto;
import ${entityPackagePath}.${changeTableName}UpdateDto;
import ${entityPackagePath}.${changeTableName}SelectDto;

import com.hchl.org.graphics.utils.UUID;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.List;
${entityOtherImport}

/**
 * ${tableAnnotation} service实现类
 *
 * @author ${author}
 * @url https://gitee.com/pucong
 * @create ${date}
 */
@Service
@Slf4j
public class ${changeTableName}ServiceImpl extends ServiceImpl<${changeTableName}Mapper,${changeTableName}> implements ${changeTableName}Service {

    @Autowired
    private ${changeTableName}Mapper ${changeTableName?uncap_first}Mapper;

    @Autowired
    private ${changeTableName}BeanConvert ${changeTableName?uncap_first}BeanConvert;

    @Override
    public IPage get${changeTableName}ListByPage(int pageNum, int pageSize, ${changeTableName}SelectDto ${changeTableName?uncap_first}Dto){
        ${changeTableName} ${changeTableName?uncap_first} = ${changeTableName?uncap_first}BeanConvert.dto2do(${changeTableName?uncap_first}Dto);
        Page<${changeTableName}> page = new Page<>(pageNum, pageSize);
        // 构造分页查询
        LambdaQueryWrapper<${changeTableName}> params = new LambdaQueryWrapper<>();
<#if modelColumn?exists>
    <#list modelColumn as model>
        <#if model.columnType = 'varchar' || model.columnType = 'text' || model.columnType = 'char' || model.columnType = 'bpchar'>
            <#if model_index = 0>
                // ${model.columnComment}
                params.eq(StringUtils.isNotBlank(${changeTableName?uncap_first}.get${model.changeColumnName}()),${changeTableName}::get${model.changeColumnName},${changeTableName?uncap_first}.get${model.changeColumnName}())<#if !model_has_next>;</#if>
            <#else>
                    //${model.columnComment}
                    .eq(StringUtils.isNotBlank(${changeTableName?uncap_first}.get${model.changeColumnName}()),${changeTableName}::get${model.changeColumnName},${changeTableName?uncap_first}.get${model.changeColumnName}())<#if !model_has_next>;</#if>
            </#if>
        <#else>
            <#if model_index = 0>
                // ${model.columnComment}
                params.eq(${changeTableName?uncap_first}.get${model.changeColumnName}() != null,${changeTableName}::get${model.changeColumnName},${changeTableName?uncap_first}.get${model.changeColumnName}())<#if !model_has_next>;</#if>
            <#else>
                    //${model.columnComment}
                    .eq(${changeTableName?uncap_first}.get${model.changeColumnName}() != null,${changeTableName}::get${model.changeColumnName},${changeTableName?uncap_first}.get${model.changeColumnName}())<#if !model_has_next>;</#if>
            </#if>
        </#if>
    </#list>
</#if>

        params.orderByAsc(${changeTableName}::get${tableIdUpper});
        IPage<${changeTableName}> ${changeTableName?uncap_first}IPage = ${changeTableName?uncap_first}Mapper.selectPage(page, params);
        return ${changeTableName?uncap_first}IPage;
    }

    @Override
    public List<${changeTableName}Vo> get${changeTableName}List(${changeTableName}SelectDto ${changeTableName?uncap_first}Dto){
        ${changeTableName} ${changeTableName?uncap_first} = ${changeTableName?uncap_first}BeanConvert.dto2do(${changeTableName?uncap_first}Dto);
        LambdaQueryWrapper<${changeTableName}> params = new LambdaQueryWrapper<>();
<#if modelColumn?exists>
    <#list modelColumn as model>
        <#if model.columnType = 'varchar' || model.columnType = 'text' || model.columnType = 'char' || model.columnType = 'bpchar'>
            <#if model_index = 0>
                // ${model.columnComment}
                params.eq(StringUtils.isNotBlank(${changeTableName?uncap_first}.get${model.changeColumnName}()),${changeTableName}::get${model.changeColumnName},${changeTableName?uncap_first}.get${model.changeColumnName}())<#if !model_has_next>;</#if>
            <#else>
                //${model.columnComment}
                .eq(StringUtils.isNotBlank(${changeTableName?uncap_first}.get${model.changeColumnName}()),${changeTableName}::get${model.changeColumnName},${changeTableName?uncap_first}.get${model.changeColumnName}())<#if !model_has_next>;</#if>
            </#if>
        <#else>
            <#if model_index = 0>
                // ${model.columnComment}
                params.eq(${changeTableName?uncap_first}.get${model.changeColumnName}() != null,${changeTableName}::get${model.changeColumnName},${changeTableName?uncap_first}.get${model.changeColumnName}())<#if !model_has_next>;</#if>
            <#else>
                //${model.columnComment}
                .eq(${changeTableName?uncap_first}.get${model.changeColumnName}() != null,${changeTableName}::get${model.changeColumnName},${changeTableName?uncap_first}.get${model.changeColumnName}())<#if !model_has_next>;</#if>
            </#if>
        </#if>
    </#list>
</#if>

        params.orderByAsc(${changeTableName}::get${tableIdUpper});
        List<${changeTableName}> ${changeTableName?uncap_first}s = ${changeTableName?uncap_first}Mapper.selectList(params);
        List<${changeTableName}Vo> ${changeTableName?uncap_first}Vos = ${changeTableName?uncap_first}BeanConvert.do2vos(${changeTableName?uncap_first}s);
        return ${changeTableName?uncap_first}Vos;
    }

    @Override
    public ${changeTableName}Vo get${changeTableName}ById(String id){
        ${changeTableName} ${changeTableName?uncap_first} = ${changeTableName?uncap_first}Mapper.selectById(id);
        ${changeTableName}Vo ${changeTableName?uncap_first}Vo = ${changeTableName?uncap_first}BeanConvert.do2vo(${changeTableName?uncap_first});
        return ${changeTableName?uncap_first}Vo;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public int insert${changeTableName}(${changeTableName}InsertDto ${changeTableName?uncap_first}Dto){
        ${changeTableName} ${changeTableName?uncap_first} = ${changeTableName?uncap_first}BeanConvert.dto2do(${changeTableName?uncap_first}Dto);
        String uuid = UUID.uuid();
        ${changeTableName?uncap_first}.set${tableIdUpper}(uuid);
        //todo
        int i = ${changeTableName?uncap_first}Mapper.insert(${changeTableName?uncap_first});
        return i;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public int update${changeTableName}ById(String id, ${changeTableName}UpdateDto ${changeTableName?uncap_first}Dto){
        ${changeTableName} ${changeTableName?uncap_first}Update = ${changeTableName?uncap_first}BeanConvert.dto2do(${changeTableName?uncap_first}Dto);
        ${changeTableName?uncap_first}Update.set${tableIdUpper}(id);
        //todo
        int i = ${changeTableName?uncap_first}Mapper.updateById(${changeTableName?uncap_first}Update);
        return i;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public int delete${changeTableName}ById(String id){
        int i = ${changeTableName?uncap_first}Mapper.deleteById(id);
        return i;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public int batchDelete${changeTableName}ByIds(List<String> ids){
        int i = ${changeTableName?uncap_first}Mapper.deleteBatchIds(ids);
        return i;
    }
}
